/*
 * Copyright (C) 2011 Nanohex Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nanohexcorp.kudocode.snapqr;

import com.nanohexcorp.kudocode.snapqr.db.SimpleDB;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SnapQRAdd extends Activity {
	private EditText m_edtTitle;
	private EditText m_edtWeb;
	private EditText m_edtDescription;
	private Button m_btnCancel;
	private Button m_btnSave;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.snapqr_add);

        setupButtons();
        setupFormControls();
        setupBundles();
    }

    private void returnToMainActivity()
    {
    	startActivityForResult(new Intent(getApplicationContext(), SnapQR.class), 0);
    }

    private void setupBundles()
    {
		Bundle extras = getIntent().getExtras();
		if(extras == null) return;
		String url = extras.getString("url");
		if(url == null) return;
		m_edtWeb.setText(url);
    }

    private void setupFormControls()
    {
    	m_edtTitle = (EditText) findViewById(R.id.add_edit_title);
    	m_edtWeb = (EditText) findViewById(R.id.add_edit_web);
    	m_edtDescription = (EditText) findViewById(R.id.add_edit_description);
    }

    private void setupButtons() {
    	m_btnCancel = (Button) findViewById(R.id.bar_cancel);
    	m_btnSave = (Button) findViewById(R.id.bar_save);

        m_btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	returnToMainActivity();
            }

        });

        m_btnSave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	if(validateForm() == true) {
            		SimpleDB.QRLinksTable.add(m_edtTitle.getText().toString(), m_edtWeb.getText().toString(), m_edtDescription.getText().toString());
            		returnToMainActivity();
            	}
            }
        });
    }

    private boolean validateForm()
    {
    	if(m_edtTitle.getText().toString().equals("")) {
    		Toast.makeText(this, "Please enter a title" , Toast.LENGTH_SHORT).show();
    		return false;
    	}
    	if(m_edtWeb.getText().toString().equals("")) {
    		Toast.makeText(this, "Please enter a URL" , Toast.LENGTH_SHORT).show();
    		return false;
    	}
    	if(!m_edtWeb.getText().toString().contains("http")) {
    		Toast.makeText(this, "Please enter a valid web address" , Toast.LENGTH_SHORT).show();
    		return false;
    	}
    	return true;
    }
}
